package com.zqh.app.manager.impl;

import com.zqh.app.dao.PushLogMapper;
import com.zqh.app.dao.PushMapper;
import com.zqh.app.dao.UserMapper;
import com.zqh.app.entity.Push;
import com.zqh.app.entity.PushLog;
import com.zqh.app.entity.User;
import com.zqh.app.manager.AppPushManager;
import com.zqh.app.utils.PushContentUtils;
import com.zqh.app.utils.jiguang.push.PushResult;
import com.zqh.app.utils.jiguang.push.model.Platform;
import com.zqh.app.utils.jiguang.push.model.audience.Audience;
import com.zqh.app.utils.PushUtils;
import com.zqh.common.utils.DateUtils;
import com.zqh.common.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("AppPushManager")
public class AppPushManagerImpl implements AppPushManager{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PushLogMapper pushLogMapper;

    @Autowired
    private PushMapper pushMapper;

    @Override
    public PushResult sendMessage( List<Push> pushes) {
//        List<Push> pushes = new ArrayList<>();
//        Push push = new Push();
//        push.setUserId(1);
//        push.setContent(content);
//        push.setType("2");
//        pushes.add(push);
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        for (Push push:pushes) {
            stringCollection.add(push.getJiguangId());
        }
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), pushes.get(0).getContent());
    }

    @Override
    public PushResult undetectedTen() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.undetectedTen(DateUtils.getTenDate(-10));
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        String content = PushContentUtils.undetectedTen.get((int)(Math.random()*PushContentUtils.undetectedTen.size()));
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content);
            pushes.add(push);
            stringCollection.add(oneUser.getJiguangId());
        }
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), content);
    }

    @Override
    public PushResult offAndOn() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.offAndOn(DateUtils.getTenDate(-2)+"%");
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        String content = PushContentUtils.offAndOn.get((int)(Math.random()*PushContentUtils.offAndOn.size()));
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content);
            pushes.add(push);
            stringCollection.add(oneUser.getJiguangId());
        }
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), content);
    }

    @Override
    public PushResult continuousFifteen() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.continuousFifteen(DateUtils.getTenDate(-1)+" 10:00:00",DateUtils.getTenDate(0)+"10:00:00");
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        String content = PushContentUtils.continuousFifteen.get((int)(Math.random()*PushContentUtils.continuousFifteen.size()));
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(oneUser.getJiguangId()), content.replace("XX",oneUser.getContinueAll()));
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content.replace("XX",oneUser.getContinueAll()));
            pushes.add(push);
        }
        pushMapper.batchSave(pushes);
        return null;
    }

    @Override
    public PushResult newUser() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.newUser(DateUtils.getTenDate(-1)+" 00:00:00",DateUtils.getTenDate(0)+"00:00:00");
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        String content = PushContentUtils.newUser.get(0);
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content);
            pushes.add(push);
            stringCollection.add(oneUser.getJiguangId());
        }
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), content);
    }

    @Override
    public PushResult lockSleepOne() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.lockSleepOne(DateUtils.getTenDate(-1)+"00:00:00");
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        String content = PushContentUtils.lockSleep.get((int)(Math.random()*PushContentUtils.lockSleep.size()));
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content);
            pushes.add(push);
            stringCollection.add(oneUser.getJiguangId());
        }
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), content);

    }

    @Override
    public PushResult lockSleep() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.lockSleep(DateUtils.getTenDate(-1)+"00:00:00");
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        String content = PushContentUtils.lockOne.get((int)(Math.random()*PushContentUtils.lockOne.size()));
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content);
            pushes.add(push);
            stringCollection.add(oneUser.getJiguangId());
        }
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), content);

    }

    @Override
    public PushResult lockOne() {
        List<Push> pushes = new ArrayList<>();
        ArrayList<User> userArrayList = userMapper.lockOne(DateUtils.getTenDate(-1)+"00:00:00");
        Collection<String> stringCollection = new LinkedHashSet();  //设备编号 registrationId
        String content = PushContentUtils.lockSleepOne.get((int)(Math.random()*PushContentUtils.lockSleepOne.size()));
        if (userArrayList.isEmpty()){
            return null;
        }
        for (User oneUser:userArrayList) {
            Push push = new Push();
            push.setType("1");
            push.setUserId(oneUser.getUserId());
            push.setContent(content);
            pushes.add(push);
            stringCollection.add(oneUser.getJiguangId());
        }
        pushMapper.batchSave(pushes);
        Map extras = new HashMap(); //extras（额外参数）
        extras.put("fType","health");
        extras.put("sType","");
        extras.put("sungoId","");//extras（额外参数）
        return PushUtils.SendPushByregistrationId(Platform.all(),extras, Audience.registrationId(stringCollection), content);

    }

    @Override
    public boolean isPush(String functionName, String updateTime) {
        PushLog pushLog = pushLogMapper.selectOneByFunctionName(functionName);
        if (pushLog.getUpdateTime().equals(updateTime)){   //时间相等，说明推送过了
            return false;
        }
        int update = pushLogMapper.updateByFunctionName(functionName,pushLog.getUpdateTime(),updateTime);  //乐观锁
        System.out.print(functionName+'|'+pushLog.getUpdateTime()+'|'+updateTime);
        System.out.print(update);
        if (update > 0){
            return true;
        }
        return false;
    }
}
